package com.worldcup.controller;

import com.worldcup.common.CommonResult;
import com.worldcup.model.Country;
import com.worldcup.model.dto.CountrySupportDTO;
import com.worldcup.service.CountryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author duwan.zq
 * @date 2018/05/31
 */
@RestController
@RequestMapping("/api/java/country")
public class CountryController {

    @Autowired
    private CountryService countryService;

    @RequestMapping("/list")
    public CommonResult listCountry(String group, String type){
        List<Country> countryList = countryService.listCountry(group);
        List<Country> result = new ArrayList<>(countryList.size());
        if("worldcup".equalsIgnoreCase(type)){
            countryList.forEach(country -> {
                if(StringUtils.isNotBlank(country.getGroupName())){
                    result.add(country);
                }
            });
            return CommonResult.buildSuccess(result);
        }else{
            return CommonResult.buildSuccess(countryList);
        }

    }

    @RequestMapping("list/limit/{limit}")
    public CommonResult listCountry(@PathVariable Integer limit){
        limit = limit == null ? 5 : limit;
        List<Country> countryList = countryService.getHotCountry(limit);
        return CommonResult.buildSuccess(countryList);
    }

    @RequestMapping(value = "/support", method = RequestMethod.POST)
    public CommonResult supportCountry(@RequestBody @Validated CountrySupportDTO countrySupportDTO){
        countryService.support(countrySupportDTO.getOpenid(), countrySupportDTO.getCountryIds());
        return CommonResult.buildSuccess();
    }

}
